package cn.jyc.ssm03.service;

import cn.jyc.ssm03.dao.RoleMapper;
import cn.jyc.ssm03.dao.UserMapper;
import cn.jyc.ssm03.pojo.Role;
import cn.jyc.ssm03.pojo.RoleMenu;
import cn.jyc.ssm03.pojo.User;
import cn.jyc.ssm03.pojo.UserRole;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RolesService {
    @Autowired
    private RoleMapper rm;
    @Autowired
    private UserMapper um;

    public List<Role> qryAll() {
        return rm.qryRole();
    }

    public List<Integer> qryURoles(Integer uid) {
        return um.qryURoles(uid);
    }

    public List<Integer> getRoleMenuId(Integer rid) {
        return rm.getRoleMenu(rid);
    }

    public Map<String, Object> saveRoleMenu(Integer rid, Integer[] mids) {
        Map<String, Object> res = new HashMap<>();
        try {
            rm.delRoleMenu(rid);
            for (Integer mid : mids) {
                rm.addRoleMenu(new RoleMenu(rid, mid));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.put("errorMsg", e.getMessage());
        }
        return res;
    }

    public Map<String, Object> getUsers(Integer page, Integer rows) {
        Map<String, Object> res = new HashMap<>();
        Page<Object> pg = PageHelper.startPage(page, rows);
        List<User> users = um.qryUsers();
        res.put("rows", users);
        res.put("total", pg.getTotal());
        return res;
    }

    public Map<String, Object> addUser(User u, Integer[] rids) {
        Map<String, Object> res = new HashMap<>();
        try {
            um.addUser(u);
            for (Integer rid : rids) {
                um.addUserRole(new UserRole(u.getUserid(), rid));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.put("errorMsg", e.getMessage());
        }
        return res;
    }

    public Map<String, Object> updUser(User u, Integer[] rids) {
        Map<String, Object> res = new HashMap<>();
        try {
            um.updUser(u);
            um.delUserRole(u.getUserid());
            for (Integer rid : rids) {
                um.addUserRole(new UserRole(u.getUserid(), rid));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.put("errorMsg", e.getMessage());
        }
        return res;
    }

    public Map<String, Object> delUser(Integer uid) {
        Map<String, Object> res = new HashMap<>();
        try {
            um.delUser(uid);
            um.delUserRole(uid);
        } catch (Exception e) {
            e.printStackTrace();
            res.put("errorMsg", e.getMessage());
        }
        return res;
    }

    public User qryUserByName(String ln, String pwd) {
        List<User> users = um.qryUserByName(ln);
        if (users.size() == 1) {
            System.out.println("service层查询到的用户为：" + users.get(0));
            if (users.get(0).getPwd().equals(pwd)) {
                return users.get(0);
            }
        }
        return null;
    }
}
